package com.hubu.game.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hubu.game.entity.converter.RoleConverter;
import com.hubu.game.entity.pojo.Role;
import com.hubu.game.entity.vo.RoleVo;
import com.hubu.game.service.IRoleService;
import com.hubu.game.utils.Result;
import com.hubu.game.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author zjj
 * @since 2023-10-24
 */
@RestController
@Slf4j
@Api("角色菜单")
@RequestMapping("/game/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    private final RoleConverter roleConverter=RoleConverter.INSTANCE;

    @GetMapping("getRoles/{userId}")
    @ApiOperation("获取当前用户的角色")
    public Result getRolesById(@PathVariable Integer userId){
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getUserId,userId);
        List<Role> roleList = roleService.list(queryWrapper);


        if(roleList.isEmpty()){
            return Result.fail(ResultCode.NO_ROLE_IN_CURRENT);
        }

        return Result.success(roleConverter.roleList2RoleVoList(roleList));
    }

    @PostMapping("addRoles")
    @ApiOperation("新增单个角色")
    public Result addRoles(@RequestBody RoleVo roleVo){
        Role role = roleConverter.roleVo2Role(roleVo);

        boolean isSaved = roleService.save(role);
        return isSaved?Result.success(ResultCode.SAVE_ROLE_SUCCESS):Result.fail(ResultCode.ADD_FAIL);
    }

    @PostMapping("addRolesList")
    @ApiOperation("新增角色列表")
    public Result addRoleList(@RequestBody List<RoleVo> roleVoList){
        List<Role> roleList = roleConverter.roleVoList2RoleList(roleVoList);
        boolean isSavedBatch = roleService.saveOrUpdateBatch(roleList);
        return isSavedBatch?Result.success(ResultCode.SAVE_ROLE_SUCCESS):Result.fail(ResultCode.ADD_FAIL);
    }

    @PostMapping("deleteRoles")
    @ApiOperation("删除单个角色")
    public Result deleteRoles(@RequestBody RoleVo roleVo){
        Role role = roleConverter.roleVo2Role(roleVo);

        boolean isRemoved = roleService.removeById(role);

        return isRemoved?Result.success():Result.fail();
    }

    @PostMapping("deleteRoleList")
    @ApiOperation("删除角色列表")
    public Result deleteRolesList(@RequestBody List<RoleVo> roleList){
        boolean isRemoved = roleService.removeBatchByIds(roleList);
        return isRemoved?Result.success():Result.fail();
    }

    @PutMapping("updateRoles")
    @ApiOperation("修改单个角色")
    public Result updateRoles(@RequestBody RoleVo roleVo){
        Role role = roleConverter.roleVo2Role(roleVo);
        boolean isUpdated = roleService.updateById(role);
        return isUpdated?Result.success():Result.fail();
    }

    @PutMapping("updateRoleList")
    @ApiOperation("修改角色列表")
    public Result updateRoleList(@RequestBody List<RoleVo> roleVoList){
        List<Role> roleList = roleConverter.roleVoList2RoleList(roleVoList);
        boolean isUpdated = roleService.updateBatchById(roleList);
        return isUpdated?Result.success():Result.fail();
    }

}
